package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.bean.ReceptionParam
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class DaoDianListPageRepository(var phone:String,var tid:String) : BasePageRepository<Data>() {
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<Data>) -> Unit) {
        ApiClient.getInstance().getPassengerF(phone,tid,1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        total.postValue(t.data!!.total)
                        t.data?.apply {
                            callback(this.rows)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Data>) {
        ApiClient.getInstance().getPassengerF(phone,tid,params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        t.data?.apply {
                            callback.onResult(this.rows, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Data>) {
        ApiClient.getInstance().getPassengerF(phone,tid,params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        t.data?.apply {
                            callback.onResult(this.rows, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }
}